﻿/*
* 此js都是调度中用到的js 
* by jijunjian
*
*
*/
// hf_curr_dids这个控件保存当前在地图的配送员编号（可能有一个或者3个）,主要用来定时更新位置(30s)
//保存格式：1。点击配送员：d_配送员编号
//          2.点击订单:o_配送员编号(可能是一个编号或者3个编号)


var usermarkers = new Array();
var shopmarkers = new Array();


//点击配送员，在地图上显示位置,订单情况
// ordernum 未处理订单个数
//name   名称
//lat 纬度
//lng 经度
//deleiverid 配送员编号
//gpsimei 配送员对应的imei
//phone
function showme(ordernum, name, lat, lng, deleiverid, gpsimei, Phone) {


    isdb = false;
    window.setTimeout(singleclick, 500)
    function singleclick() {
        if (isdb != false) {
            return false;
        }

        //获取配送员当前位置
        map.clearOverlays();
        var url = "../ajax/GetDeliverModel.aspx";
        var para = "id=" + deleiverid + "&t=" + new Date().getTime();
        jQuery.ajax(
            {
                type: "post",
                url: url,
                data: para,
                success: function (msg) {
                    //以配送员为中心
                    var deli_latlng = eval("(" + msg + ")");

                    var d_point = new BMap.Point(deli_latlng.lng, deli_latlng.lat); // 创建点坐标

                    $("#hidLat").val(deli_latlng.lat);
                    $("#hidLng").val(deli_latlng.lng);

                    var d_marker = new BMap.Marker(d_point, { icon: myIcon });
                    d_marker.setTitle(deli_latlng.d_name + "(未处理订单" + deli_latlng.ordernum + "个)");
                    map.addOverlay(d_marker);

                    $("#hf_curr_dids").val("d_" + deleiverid);

                    var opts = {
                        width: 350,     // 信息窗口宽度  
                        height: 100    // 信息窗口高度
                    }

                    var winhtml = "<div>";
                    winhtml += ' <table width="330px" border="0" cellpadding="0" cellspacing="0" class="delivertipbox">'
                    winhtml += '<tr><td>姓名：' + deli_latlng.d_name + '</td><td>电话：' + deli_latlng.Phone + '</td></tr>'
                    winhtml += '<tr><td>车况：' + deli_latlng.carstate + '<img id=\"theimage\" border=\"0\" src=\"direction.jpg\" onload=\"rotateRight(\'theimage\',' + deli_latlng.direction + ');\" /></td><td>订单：' + deli_latlng.ordernum + '</td></tr>'

                    winhtml += ' <tr><td colspan="2">商家名称：' + deli_latlng.TogoName + '</td></tr>'
                    winhtml += ' <tr><td colspan="2">商家地址：' + deli_latlng.TogoAddress + '</td></tr>'
                    winhtml += ' <tr><td colspan="2">客户地址：' + deli_latlng.AddressText + '</td></tr>'


                    winhtml += " </table></div>";

                    var infoWindow = new BMap.InfoWindow(winhtml, opts);  // 创建信息窗口对象
                    // 打开信息窗口
                    d_marker.openInfoWindow(infoWindow)

                    map.panTo(d_point);

                    d_marker.addEventListener("click", function (e) {
                        this.openInfoWindow(infoWindow);
                    });


                    showAllShopAndUser(deli_latlng.orderlist)



                }
            })
    }
}

///显示配送员订单中商家，用户位置。
function showAllShopAndUser(orders)
{
    if (usermarkers.length > 0) {
        for (var i in usermarkers)
        {
            map.removeOverlay(usermarkers[i]);
        }
        usermarkers.length = 0;
    }

    if (shopmarkers.length > 0) {
        for (var i in shopmarkers) {
            map.removeOverlay(shopmarkers[i]);
        }
        shopmarkers.length = 0;
    }

    for (var i in orders) 
    {

        var jsonlatlng = eval("(" + orders[i].latlng + ")");
        //用户坐标
        map.removeOverlay(marker);
        var userpoint = new BMap.Point(jsonlatlng.ulng, jsonlatlng.ulat); // 创建点坐标
        var userIcon = new BMap.Icon("images/tack.gif", new BMap.Size(33, 33), { anchor: new BMap.Size(17, 0) });
        var usermarker = new BMap.Marker(userpoint, { icon: userIcon });

        var userlabel = new BMap.Label(i+"用户：" + orders[i].AddressText, { offset: new BMap.Size(-28, -22) });
        userlabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
        usermarker.setLabel(userlabel);

        usermarkers.push(usermarker);

        map.addOverlay(usermarker);
        //商家坐标
        var shoppoint = new BMap.Point(jsonlatlng.slng, jsonlatlng.slat); // 创建点坐标
        var shopmarker = new BMap.Marker(shoppoint);

        var shoplabel = new BMap.Label(i + "商家：" + orders[i].TogoAddress, { offset: new BMap.Size(-28, -22) });
        shoplabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
        shopmarker.setLabel(shoplabel);

        shopmarkers.push(shopmarkers);

        map.addOverlay(shopmarker);

    }
   
}

/*
* 点击订单，如果已经分配，显示
* by jijunjian
* state 订单状态
*laglng 用户，商家地址（json）
*deliverid 配送员编号
*orderid 订单编号
*username 用户名称，
address 用户地址
shopname 商家名称
sectionid 订单区域编号
*/
function showorderlocal(state, _latlng, deliverid, orderid, username, address, shopname, sectionid,togoid) {
    map.clearOverlays();
    $("#hforderid").val(orderid);
    $("#hfordersection").val(sectionid)
    $("#hfstate").val(state);;


    var jsonlatlng = eval("(" + $("#latlng_" + orderid).html() + ")");
    //用户坐标
    map.removeOverlay(marker);
    var userpoint = new BMap.Point(jsonlatlng.ulng, jsonlatlng.ulat); // 创建点坐标
    var userIcon = new BMap.Icon("images/tack.gif", new BMap.Size(33, 33), { anchor: new BMap.Size(17, 0) });
    var usermarker = new BMap.Marker(userpoint, { icon: userIcon });

    var userpre = "用户:";
    var shoppre = "商家:";
    if (togoid == 0) {
         userpre = "发件:";
         shoppre = "收件:";
    }

    var userlabel = new BMap.Label(userpre + username + "(" + address + ")", { offset: new BMap.Size(-28, -22) });
    userlabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
    usermarker.setLabel(userlabel);

    map.addOverlay(usermarker);
    //商家坐标
    var shoppoint = new BMap.Point(jsonlatlng.slng, jsonlatlng.slat); // 创建点坐标
    var shopmarker = new BMap.Marker(shoppoint);

    var shoplabel = new BMap.Label(shoppre + shopname, { offset: new BMap.Size(-28, -22) });
    shoplabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
    shopmarker.setLabel(shoplabel);

    map.addOverlay(shopmarker);

    var scope = request("scope");
    if (scope.length == 0) {
        scope = "0";
    }

    var scopeparams = "&scope=" + scope;

    switch (state) {
        case 3:
        case 6:
        case 4:
        case 5:
            {
                return;
                break;
            }
        case 1:
        case 2: //在地图上显示位置,用户，商家,还有推荐的3个配送员(离商家最近的),以商家为中心
            {
                map.panTo(shoppoint);
                $("#hforderuserlatlng").val(jsonlatlng.slat + "," + jsonlatlng.slng);
                //显示3个最近的配送员
                var sname = $("#ddlsection option:selected").text();
                var hfcityid = $("#hfcityid").val();
                var url = "/Admin/ajax/GetDeliverListbySpan.aspx";
                var para = "sname=" + escape(sname) + "&t=" + new Date().getTime() + "&lat=" + jsonlatlng.slat + "&lng=" + jsonlatlng.slng + "&cid=" + hfcityid + scopeparams;


                var searchdeliverurl = url + "?type=search&" + para;
                $("#tbdeliver").autocomplete(searchdeliverurl);

                showNearDelivers(url, para);



                break;

            }

        case 7: //在地图上显示位置,用户，商家，配送员

            map.panTo(shoppoint);
            $("#hforderuserlatlng").val(jsonlatlng.slat + "," + jsonlatlng.slng);
            //显示3个最近的配送员
            var sname = $("#ddlsection option:selected").text();
            var hfcityid = $("#hfcityid").val();
            var url = "../ajax/GetDeliverListbySpan.aspx";
            var para = "sname=" + escape(sname) + "&t=" + new Date().getTime() + "&lat=" + jsonlatlng.slat + "&lng=" + jsonlatlng.slng + "&cid=" + hfcityid + scopeparams;



            var searchdeliverurl = url + "?type=search&" + para;
            $("#tbdeliver").autocomplete(searchdeliverurl);

            showNearDelivers(url, para);


            if (deliverid == 0) {

                hideload_super('dddd');
                break;
            }
            var url = "../ajax/GetDeliverModel.aspx";
            var para = "id=" + deliverid + "&t=" + new Date().getTime();
            jQuery.ajax(
                {
                    type: "post",
                    url: url,
                    data: para,
                    success: function (msg) {
                        //以配送员为中心
                        var deli_latlng = eval("(" + msg + ")");

                        var d_point = new BMap.Point(deli_latlng.lng, deli_latlng.lat); // 创建点坐标

                        var d_icon = new BMap.Icon("images/mapmarker/marker" + (parseInt(1)) + ".png", new BMap.Size(20, 34), { anchor: new BMap.Size(10, 0) });
                        var d_marker = new BMap.Marker(d_point, { icon: d_icon });

                        var deliverlabel = new BMap.Label(deli_latlng.d_name + "(" + deli_latlng.ordernum + ")", { offset: new BMap.Size(-28, -22) });

                        deliverlabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
                        d_marker.setLabel(deliverlabel);
                        map.addOverlay(d_marker);

                        d_marker.setTitle(deliverid);

                        d_marker.addEventListener("click", function (e) {
                            showDeliverBox(this.getTitle(), this);
                        });


                        map.addOverlay(d_marker);

                        map.panTo(d_point);
                        $("#hf_curr_dids").val("o_" + deliverid);
                        hideload_super('dddd');



                    }
                })


            break;
    }
    $("html,body").animate({ scrollTop: 0 }, 1000);

}

function showNearDelivers(url, params) {
    jQuery.ajax(
    {
        type: "get",
        url: url,
        data: params,
        success: function (msg) {
            var d_list = eval("(" + msg + ")");
            var ddl_d = document.getElementById("ddldeliver");
            ddl_d.options.length = 0;

            for (var i = 0; i < d_list.length; i++) {
                if (i < 3) {   //前3个添加标注
                    if (d_list[i].distance != "99999999") {

                        var d_point = new BMap.Point(d_list[i].Lng, d_list[i].Lat); // 创建点坐标
                        var d_icon = new BMap.Icon('/Admin/Service/images/mapmarker/marker_' + d_list[i].workstate + '.png', new BMap.Size(20, 34), { anchor: new BMap.Size(10, 0) });
                        var d_marker = new BMap.Marker(d_point, { icon: d_icon });

                        var deliverlabel = new BMap.Label(d_list[i].Name + "(" + d_list[i].OrderNum + ")", { offset: new BMap.Size(-28, -22) });
                        deliverlabel.setStyle({ color: "blue", fontSize: "12px", padding: "2px 5px 2px 5px" });
                        d_marker.setLabel(deliverlabel);
                        map.addOverlay(d_marker);
                        markerlist.push(d_marker);
                        d_marker.setTitle(d_list[i].DataId);

                        d_marker.addEventListener("click", function (e) {
                            showDeliverBox(this.getTitle(), this);
                        });

                        //初始化右边配送员信息
                        if (i == 0) {
                            $("#hfdid_hh").val(d_list[0].DataId);
                        }
                    }
                }

                ddl_d.options.add(new Option(d_list[i].Name, d_list[i].DataId));
            }
            hideload_super('dddd');
            getsubsort();
        
        }
    })
}


///返回订单信息,和进度信息
function getorderinfo(orderid) {
    var url = "ajax/orderinfoandprocesser.aspx";
    var para = "orderid=" + orderid + "&t=" + new Date().getTime();
    jQuery.ajax(
    {
        type: "post",
        url: url,
        data: para,
        success: function (msg) {
            var data = eval("(" + msg + ")");
            $("#tborderinfo").val(data.messagebody);
        }
    })
}


///发短信验证
function checkmsg() {
    var d_id = $("#hfdid_hh").val() + "";
    if (d_id == "0") {
        alert("请选择配送员。");
        return false;
    }
    var tborderinfo = $("#tborderinfo").val() + "";
    if (tborderinfo == "") {
        alert("请输入短信内容");
        return false;
    }
    showload_super("", "", "dddd");
    return true;
}

///发短信验证
function checkdeliver() {
    var d_id = $("#hfdid_hh").val() + "";
    if (d_id == "0") {
        alert("请选择配送员。");
        return false;
    }
    showload_super("", "", "dddd");
    return true;
}

///发群验证
function checksendgroup() {
    var d_id = $("#ddlgroup").val() + "";
    if (d_id == "0") {
        alert("请选择群组。");
        return false;
    }

    showload_super("", "", "dddd");
    return true;
}

//选择配送员
function getsubsort() {
    $("#hfdid_hh").val(document.getElementById("ddldeliver").value);
}

//各个提醒
function Tellmsg() {
    var hfcityid = $("#hfcityid").val();
    var url = "ajax/mymsg.aspx";
    var para = "t=" + new Date().getTime() + "&cid=" + hfcityid;
    jQuery.ajax(
    {
        type: "get",
        url: url,
        data: para,
        success: function (msg) {
            if (msg != "0") {
                 
                var counts = msg.split(",");

                //$.jBox.messager(msg, '温馨提醒', 0);

                if ( parseInt(counts[0]) > 0) {
                    $("#neworderAudio")[0].play();
                }
                if (parseInt(counts[1]) > 0) {
                    setTimeout(" $('#chatAudio')[0].play()",2000);
                   
                }
                if (parseInt(counts[2]) > 0) {
                    setTimeout(" $('#newweixinorderaudio')[0].play()", 2000);
                }
               
            }
        }
    })

    LoadDeliver(9);
}



//图片旋转
/*
id 图片id
angle 度数  
*/
function rotate(id, angle, whence) {
    var p = document.getElementById(id);

    // we store the angle inside the image tag for persistence  
    if (!whence) {
        p.angle = ((p.angle == undefined ? 0 : p.angle) + angle) % 360;
    } else {
        p.angle = angle;
    }

    if (p.angle >= 0) {
        var rotation = Math.PI * p.angle / 180;
    } else {
        var rotation = Math.PI * (360 + p.angle) / 180;
    }
    var costheta = Math.cos(rotation);
    var sintheta = Math.sin(rotation);

    if (document.all && !window.opera) {
        var canvas = document.createElement('img');

        canvas.src = p.src;
        canvas.height = p.height;
        canvas.width = p.width;

        canvas.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11=" + costheta + ",M12=" + (-sintheta) + ",M21=" + sintheta + ",M22=" + costheta + ",SizingMethod='auto expand')";
    } else {
        var canvas = document.createElement('canvas');
        if (!p.oImage) {
            canvas.oImage = new Image();
            canvas.oImage.src = p.src;
        } else {
            canvas.oImage = p.oImage;
        }

        canvas.style.width = canvas.width = Math.abs(costheta * canvas.oImage.width) + Math.abs(sintheta * canvas.oImage.height);
        canvas.style.height = canvas.height = Math.abs(costheta * canvas.oImage.height) + Math.abs(sintheta * canvas.oImage.width);

        var context = canvas.getContext('2d');
        context.save();
        if (rotation <= Math.PI / 2) {
            context.translate(sintheta * canvas.oImage.height, 0);
        } else if (rotation <= Math.PI) {
            context.translate(canvas.width, -costheta * canvas.oImage.height);
        } else if (rotation <= 1.5 * Math.PI) {
            context.translate(-costheta * canvas.oImage.width, canvas.height);
        } else {
            context.translate(0, -sintheta * canvas.oImage.width);
        }
        context.rotate(rotation);
        context.drawImage(canvas.oImage, 0, 0, canvas.oImage.width, canvas.oImage.height);
        context.restore();
    }
    canvas.id = p.id;
    canvas.angle = p.angle;
    p.parentNode.replaceChild(canvas, p);
}

function rotateRight(id, angle) {
    rotate(id, angle == undefined ? 90 : angle);
}

function rotateLeft(id, angle) {
    rotate(id, angle == undefined ? -90 : -angle);
}


///返回订单信息,和进度信息
function getexpressorderinfo(orderid) {
    var url = "ajax/expressorderinfo.aspx";
    var para = "orderid=" + orderid + "&t=" + new Date().getTime();
    jQuery.ajax(
    {
        type: "post",
        url: url,
        data: para,
        success: function (msg) {
            var data = eval("(" + msg + ")");
            $("#tborderinfo").val(data.messagebody);
        }
    })
}


function showDeliverBox(deleiverid, delivermarker) {
    var url = "../ajax/GetDeliverModel.aspx";
    var para = "id=" + deleiverid + "&t=" + new Date().getTime();
    jQuery.ajax(
        {
            type: "post",
            url: url,
            data: para,
            success: function (msg) {
                //以配送员为中心
                var deli_latlng = eval("(" + msg + ")");

                var d_point = new BMap.Point(deli_latlng.lng, deli_latlng.lat); // 创建点坐标

                $("#hidLat").val(deli_latlng.lat);
                $("#hidLng").val(deli_latlng.lng);


                var opts = {
                    width: 350,     // 信息窗口宽度  
                    height: 100    // 信息窗口高度
                }

                var winhtml = "<div>";
                winhtml += ' <table width="330px" border="0" cellpadding="0" cellspacing="0" class="delivertipbox">'
                winhtml += '<tr><td>姓名：' + deli_latlng.d_name + '</td><td>电话：' + deli_latlng.Phone + '</td></tr>'
                winhtml += '<tr><td>车况：' + deli_latlng.carstate + '<img id=\"theimage\" border=\"0\" src=\"direction.jpg\" onload=\"rotateRight(\'theimage\',' + deli_latlng.direction + ');\" /></td><td>订单：' + deli_latlng.ordernum + '</td></tr>'

                winhtml += ' <tr><td colspan="2">商家名称：' + deli_latlng.TogoName + '</td></tr>'
                winhtml += ' <tr><td colspan="2">商家地址：' + deli_latlng.TogoAddress + '</td></tr>'
                winhtml += ' <tr><td colspan="2">客户地址：' + deli_latlng.AddressText + '</td></tr>'
                winhtml += " </table></div>";
                var infoWindow = new BMap.InfoWindow(winhtml, opts);  // 创建信息窗口对象
                delivermarker.openInfoWindow(infoWindow)
                map.panTo(d_point);

                //显示配送员订单中商家，用户位置。

                    showAllShopAndUser(deli_latlng.orderlist)
                


            }
        })
}